﻿<?php 
if (!defined('WEB_ROOT')) {
	exit;
}

if ($pcat == 'gttl' || $pcat == 'hpsp')
{
	require_once 'item_list.php';
	return;
}

$psubcat   = (isset($_GET['subcat']) && $_GET['subcat'] != '') ? $_GET['subcat'] : '';
$tagName = (isset($_GET['tag']) && $_GET['tag'] != '') ? $_GET['tag'] : '';


$subcatCond = '';
if ($pcat != 'kyht' && $pcat != 'bvtg' && $psubcat != '')
{
	$subcatCond = " IN (".str_replace("_", ",", $psubcat).")";
}
$sql = '';
$catIdCol = '';
$catTitle = getTitle($pcat, $psubcat);
$tagCond = '';
if ($tagName != '') 
{
	//$tagName = $_POST['catName'];
	$catTitle = $mapCat[$pcat]." có nhãn &#171;".$tagName."&#187;";
	$tagCond = " JOIN tag ON tag.item_id = mm.id AND tag.tag = '".$tagName."' ";
}
$summary_text_length = SUMMARY_TEXT_LENGTH;
$sqlSuffix = '';
switch ($pcat)
{
	case 'bb':
		$sql = "SELECT bb.id, bb.tenbb as ten, SUBSTR(bb.tomtat_bb, 1, $summary_text_length) as tomtat, bb.ngay_dang, GROUP_CONCAT(DISTINCT hotentg) cac_tg,
			GROUP_CONCAT(DISTINCT tg.id) as cac_tgid 
			FROM baibao bb 
			JOIN tacgia_baibao tb ON bb.id = tb.bb_id 
			JOIN tacgia tg ON tg.id = tb.tg_id 
			JOIN chitiet_tapchi cttc ON cttc.id = bb.cttc_id
			JOIN tapchi tc ON tc.id = cttc.tc_id ";
		$catIdCol = "tc_id";
		$sqlSuffix = "GROUP BY bb.id";
		break;
	case 'dt':
		$sql = " SELECT dt.id, dt.tendt as ten, SUBSTR(dt.tomtat_dt, 1, $summary_text_length) as tomtat, dt.ngay_dang,
					GROUP_CONCAT(DISTINCT hotentg) cac_tg,
					GROUP_CONCAT(DISTINCT tg.id) as cac_tgid 
				  FROM detai dt JOIN tacgia_detai td ON dt.id = td.dt_id
					JOIN tacgia tg ON tg.id = td.tg_id ";
		$catIdCol = "cap_id";
		$sqlSuffix = "GROUP BY dt.id ";
		break;
	case 'ht':
		// Neu truong_id = NULL nghia la don vi thuc hien hoi thao la nha` truong, con neu <> NULL thi query luon ten truong (tendvt)
		// loaiht_id <> 5 nghia la tat ca hoi thao ko phai la hoi thao sinh hoat hoc thuat (seminar, hoi thao nho, ko ke ten ra day)
		$sql = " SELECT mm.id, tenht as ten, chude, mm.diadiem, ngaythuchien, ngay_dang, dv.tendv, dvt.tendv as tendvt, mm.dv_id
		FROM hoithao mm join loaihoithao lht on mm.loaiht_id = lht.id 
		JOIN donvi dv ON dv.id = mm.dv_id 
		LEFT JOIN donvi dvt ON dv.truong_id = dvt.id WHERE mm.loaiht_id <> 5 ";
		$catIdCol = "loaiht_id";
		break;
	case 'kyht':
		if ($psubcat == '')
		{
			// Neu truong_id = NULL nghia la don vi thuc hien hoi thao la nha` truong, con neu <> NULL thi query luon ten truong (tendvt)
			// loaiht_id = 4 nghia la cac hoi thao cua truong
			$sql = " SELECT mm.id, tenht as ten, chude, mm.diadiem, ngaythuchien, ngay_dang, dv.tendv, dvt.tendv as tendvt, mm.dv_id, mm.tc_id
			FROM hoithao mm join loaihoithao lht on mm.loaiht_id = lht.id 
			JOIN donvi dv ON dv.id = mm.dv_id 
			LEFT JOIN donvi dvt ON dv.truong_id = dvt.id WHERE mm.loaiht_id > 0 ";
			$catIdCol = "loaiht_id";
		}
		else
		{
			$sql = " SELECT bb.id, bb.tenbb as ten, SUBSTR(bb.tomtat_bb, 1, $summary_text_length) as tomtat, bb.ngay_dang
					FROM baibao bb 
					JOIN chitiet_tapchi cttc ON bb.cttc_id = cttc.id
					JOIN tapchi tc ON cttc.tc_id = tc.id
					WHERE tc.id = $psubcat ";
			
		}
		//echo "<br />"."<br />"."<br />".$sql;
		break;
	case 'tcnb':
		// tap chi noi bo
		if ($psubcat == "") {
			$sql = "SELECT cttc.id, tentc as ten, so, ngayra, sotrang, ngay_dang
			FROM tapchi mm	
			JOIN chitiet_tapchi cttc ON cttc.tc_id = mm.id
			JOIN loaitapchi ltc on mm.loaitc_id = ltc.id ";
		} else {
			$sql = " SELECT bb.id, bb.tenbb as ten, SUBSTR(bb.tomtat_bb, 1, $summary_text_length) as tomtat, bb.ngay_dang
					FROM baibao bb 
					JOIN chitiet_tapchi cttc ON bb.cttc_id = cttc.id
					JOIN tapchi tc ON cttc.tc_id = tc.id
					WHERE tc.id = $psubcat ";
		}
		break;
	case 'bvtg':
		$sql = getAllArticlesByAuthorId($psubcat);
		break;
	default:
		break;
}
if ($pcat != 'kyht' && $pcat != 'bvtg' && $catIdCol != '' && $subcatCond != '')
{
	$subcatCond = " WHERE " . $catIdCol . $subcatCond;
}

$sql .= " $tagCond $subcatCond $sqlSuffix " . " ORDER BY ngay_dang DESC";
//echo $sql;
//echo "<br />"."<br />"."<br />".getPagingQuery($sql, NUM_ITEMS_PER_PAGE);
$result = dbQuery(getPagingQuery($sql, NUM_ITEMS_PER_PAGE));
if ($psubcat == '') {
	$pagingLink = getPagingLink($sql, NUM_ITEMS_PER_PAGE, "view=list&cat=$pcat");
} else {
	$pagingLink = getPagingLink($sql, NUM_ITEMS_PER_PAGE, "view=list&cat=$pcat&subcat=$psubcat");
}

//$result = array();
//$result[] = array('tenbb'=>'ten bai bao', 'tomtat_bb'=>'tom tat bai bao');
$count = dbNumRows($result);

$tpcat = $pcat;
if ($cat == 'kyht' && $psubcat != '') $tpcat = 'bb';
$tenCol = 'ten';
$tomtatCol = 'tomtat';
$tomtat = '';

//echo $count;
//var_dump($result);
?>

<div id="cat_<?php echo $pcat."_".$psubcat; ?>" class="cat-widget">
	<h3>
		<a title="" href="javascript:<?php if ($psubcat != '') echo "viewList('".$pcat."','".$catTitle."','".$psubcat."');"; else echo "viewTag('".$pcat."','".$catTitle."','".$tagName."');"; ?>"><?php echo $catTitle; ?></a>
		<a class="minimize"></a>
	</h3>
	<div class="container">
		<div class="mpost">
			<ul class="list_articles">
			<?php for ($item = 0; $item < $count; $item++) { 
				$row = dbFetchAssoc($result);
				$linkDetail = "javascript:viewArticle('bb', $row[id])";
				$ten = $row[$tenCol];
				if ($pcat == 'tcnb') {
					//$tomtat = $row[];
					$ten = $ten . " số " . $row['so'];
					if ($psubcat == '') {
						$linkDetail = "javascript:viewList('$pcat', '$ten', $row[id])";
					} else {
						$tomtat = getTomtat($row[$tomtatCol]);
						$linkDetail = "javascript:viewArticle('bb', $row[id])";
					}
				} else if ($pcat != 'ht' && $pcat != 'kyht')
				{
					$tomtat = getTomtat($row[$tomtatCol]);
					$linkDetail = "javascript:viewArticle('$pcat', $row[id])";
				}
				else
				{
					if ($pcat == 'kyht')
					{
						if ($psubcat == '') {
							$ten = 'Kỷ yếu Hội thảo '.$ten;
							$linkDetail = "javascript:viewList('$pcat', '$ten', $row[id])";
						} else {
							// link de hien thi noi dung bai bao thuoc ky yeu hoi thao (kyht)
							$linkDetail = "javascript:viewArticle('bb', $row[id])";
						}
					}
					
					if ($psubcat == '') {
						$tomtat = "<span style='float:left;width:100px'>Ngày tổ chức: </span>".$row['ngaythuchien'];
						$tomtat .= "<br /><span style='float:left;width:100px'>Chủ đề: </span>"."".$row['chude']."";
						$tomtat .= "<br /><span style='float:left;width:100px'>Địa điểm: </span>"."".$row['diadiem']."";					
						$donvi = '';
						if ($row['tendvt'] == NULL)
						{
							$donvi = $row['tendv'];
						}
						else
						{
							$donvi = "Khoa: " . $row['tendv'] . " <br />Trường: " . $row['tendvt'];
						}
						$tomtat .= "<br /><span style='float:left;width:100px;height:20px;'>Đơn vị Tổ chức: </span>"."".$donvi."";
					} else {
						$tomtat = getTomtat($row[$tomtatCol]);
					}
				}
			?>
			<li class="list_articles_item">
				<div class="thumb">
					<a href="<?php echo $linkDetail; ?>">
						<img src="images/common/article_default.jpg" width="100" height="75"></img>
					</a>
					<p style="float:right;color:grey;"><?php echo $row['ngay_dang']; ?></p> 
				</div>
				<div class="info">
					<a class="dark_perm" title="<?php echo $ten; ?>" href="<?php echo $linkDetail; ?>">
						<?php echo $ten; ?>
					</a>
					<p style="color:black;"><?php echo $tomtat; ?></p><div class="clear"></div>
				</div>
				<?php if ($psubcat != '' || ($pcat != 'ht' && $pcat != 'kyht' && $pcat != 'tcnb')) { ?>
				<ul class="ex">
					<?php 
						$cac_tg = $row['cac_tg'];
						if ($cac_tg != NULL && $cac_tg != '')
						{
							$cac_tgid = $row['cac_tgid'];
							$tacgias = explode(",", $cac_tg);
							$tacgia_ids = explode(",", $cac_tgid);
					?>
					<li class="lil" style="">Tác giả: 
					<?php
							for ($tgIdx = 0; $tgIdx < count($tacgias); $tgIdx++)
							{
								// bvtg: bai viet cua tac gia, ham y hien thi tat ca bai viet (bai bao / de tai) cua tac gia do
								echo "<a href='javascript:viewList(\"bvtg\", \"\", $tacgia_ids[$tgIdx]);'>$tacgias[$tgIdx]</a> ";
								if ($tgIdx < count($tacgias) - 1)
								{
									echo "- ";
								}
							}
						}
					?>
					</li>
					<li class="cont"><a href="<?php echo $linkDetail; ?>">Ðọc tiếp</a></li>
					<div class="clear"></div>
				</ul><div class="clear"></div>
				<?php } ?>
			</li>
			<?php } ?>
			</ul>
			
			<?php if ($pagingLink != '') { ?>
			<div class="pagination-bar">
				<div class="pagination"><?php echo $pagingLink; ?></div>
				<div class="clear"></div>
			</div>
			<?php } ?>
		</div>
	</div>
	<div class="footer"></div>
	<div class="clear"></div>
</div>
